package com.acs.player.views.playlist;

import java.util.List;

import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.acs.player.PlaylistActivity;
import com.acs.player.R;
import com.acs.player.entities.Album;
import com.acs.player.entities.Artiste;
import com.acs.player.entities.Music;
import com.acs.player.entities.Playlist;


public class PlaylistAdapter extends BaseExpandableListAdapter {
	
	private List<Playlist> playlists;
	private LayoutInflater inflater;
	private PlaylistActivity playlistActivity;
	private View currentRowSelected;
	
	public PlaylistAdapter(PlaylistActivity playlistActivity, List<Playlist> playlists) {
		this.playlistActivity = playlistActivity;
		this.playlists = playlists;
		inflater = LayoutInflater.from(playlistActivity);
	}
	
	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	@Override
	public Music getChild(int playlistPosition, int musicPosition) {
		Playlist playlist = playlists.get(playlistPosition);
		List<Music> musicList = playlist.getMusicList();
		return musicList.get(musicPosition);
	}

	@Override
	public long getChildId(int playlistPosition, int musicPosition) {
		return musicPosition;
	}

	@Override
	public View getChildView(final int playlistPosition, final int musicPosition,
		boolean isLastMusic, View convertView, ViewGroup parent) {

		View view = convertView;		
		if (view == null) {
			view = inflater.inflate(R.layout.row_music, null);
		} 

		Music music = (Music) getChild(playlistPosition, musicPosition);		
        if (music != null) {
        	
        	//SET COVER
    		ImageView imageView = (ImageView) view.findViewById(R.id.rowIcon); 		
    		Album album = music.getAlbulm();
    		if(album != null){
	    		String albumArtPath = album.getImagePath();    			
	    		if(!albumArtPath.equals("")){
	    			Uri artworkUri = Uri.parse(albumArtPath);
	    			imageView.setImageURI(artworkUri);
	    		} else if(albumArtPath.equals("")) 
	    		{
	    			Log.d("sisi", "On est la");
	    			imageView.setBackgroundResource(R.drawable.no_cover);
	    		}
    		} else {
    			imageView.setBackgroundResource(R.drawable.no_cover);
    		}

    		//SET TITRE
            TextView textTitre = (TextView) view.findViewById(R.id.rowTitre);       
            if (textTitre != null) {
            	textTitre.setText(music.getTitre());  
            }
            
            //SET ARTISTE NAME
            TextView textArtiste = (TextView) view.findViewById(R.id.rowArtiste);      
            if(textArtiste != null){
            	Artiste artiste = music.getArtiste();
            	if(artiste != null){
	            	String artisteName = artiste.getNom();
	            	if(artisteName != null){
	            		textArtiste.setText(artisteName);
	            	}
            	}
            }
          //SET ARTISTE NAME
            TextView textAlbum = (TextView) view.findViewById(R.id.rowAlbum);      
            
            	if(album != null){
	            	String albumName = album.getNom();
	            	if(albumName != null){
	            		textAlbum.setText(albumName);
	            	}
            	
            }
        }
    
	    view.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {	
				view.setBackgroundColor(Color.YELLOW);			
				if(currentRowSelected != null){
					currentRowSelected.setBackgroundColor(Color.BLACK);
				}
				
				currentRowSelected = view;						
				playlistActivity.goToPlayerActivity(playlistPosition, musicPosition);
			}
		});
	
	    return view;
	}

	@Override
	public int getChildrenCount(int playlistPosition) {
		return playlists.get(playlistPosition).getMusicList().size();
	}

	@Override
	public Playlist getGroup(int playlistPosition) {
		return playlists.get(playlistPosition);
	}

	@Override
	public int getGroupCount() {
		return playlists.size();
	}

	@Override
	public long getGroupId(int playlistPosition) {
		return playlistPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

	        Playlist playlist = (Playlist) getGroup(groupPosition);
	 
	        View view = convertView;
	        if (convertView == null) {
	            view = inflater.inflate(R.layout.row_expendable_playlist, null);
	        } 

	        TextView textTitre = (TextView) view.findViewById(R.id.TVGroup);
	        textTitre.setText(playlist.getNom());
	 
	        return view;
	 }

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}
	
   class GroupViewHolder {
	    public TextView textViewGroup;
	}
	   	 
    class ChildViewHolder {
        public TextView textViewChild;
        public Button buttonChild;
    }

}
